<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px;">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="销售单号">
                <a-input v-model="queryParam.supplierName" placeholder="请输入销售单号"/>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 新增 -->
      <biz-after-sale-order-form v-if="showAddModal" ref="bizAfterSaleOrderForm"  @ok="getList" @close="showAddModal = false" />
      <!-- 处理人 -->
      <biz-after-sale-order-user v-if="showAddModal" ref="bizAfterSaleOrderUser"  @ok="getList" @close="showAddModal = false" />
      <biz-order-add-form v-if="showOrderViewModal" ref="bizOrderAddForm" @close="showOrderViewModal = false" :customerIdOptions="customerIdOptions"/>

      <advance-table
        :customRow="customRow"
        :components="drag(columns)"
        title="售后单" :pagination="{
        current: queryParam.pageNum,
        pageSize: queryParam.pageSize,
        total: total,
        showSizeChanger: true,
        showLessItems: true,
        showQuickJumper: true,
        showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
        onChange: changeSize,
        onShowSizeChange: onShowSizeChange
      }" tableKey="base-bizOrder-index-table" @change="handleTableChange" rowKey="id" @refresh="getList"
        :columns="columns" :data-source="dataList" :loading="loading" :format-conditions="true">
        <div class="table-operations" slot="button">
          <a-button type="primary" @click="handleAdd({})" v-hasPermi="['biz:bizAfterSaleOrder:add']">
            <a-icon type="plus" />新增
          </a-button>
          <a-button type="primary" v-hasPermi="['biz:bizAfterSale:export']" :loading="downLoading"  @click='exportExcel'>
            <a-icon />导出
          </a-button>
        </div>
        <span slot="showOrder" slot-scope="{record}">
          <a @click="handleShow(record,4)" style="color:#2f54eb">{{ record.orderNumber }}</a>
        </span>
        <span slot="inImg" slot-scope="{record}">
          <a @click="handleShow(record,1)" style="color:#2f54eb">浏览照片</a>
        </span>
        <span slot="cuImg"  slot-scope="{record}">
          <a @click="handleShow(record,2)" style="color:#2f54eb">浏览照片</a>
        </span>
        <span slot="saleImg"  slot-scope="{record}">
          <a @click="handleShow(record,3)" style="color:#2f54eb">浏览照片</a>
        </span>
        <span slot="userSlot" slot-scope="{record}">
          <a v-if="record.status == 0">待处理</a>
          <a else>{{ record.processUserName }}</a>
        </span>
        <span slot="statusSlot" slot-scope="{record}">
          <a v-if="record.status == 0">待处理</a>
          <a v-if="record.status == 1">处理中</a>
          <a v-if="record.status == 2">已处理</a>
        </span>
        <span slot="operation" slot-scope="{record}">
          <a-divider type="vertical" v-hasPermi="['biz:bizOrder:remove']" />
          <a @click="handleUpdate(record)" v-hasPermi="['biz:bizAfterSaleOrder:edit']" v-if="record.status == 0 && !record.accountCode">
            修改
          </a>
          <a @click="handleUser(record)" v-hasPermi="['biz:bizAfterSaleOrder:edit']" v-if="record.status == 0">
            选择处理人
          </a>
          <a v-else-if="record.status == 1">
            处理中
          </a>
          <a v-else>
            已处理
          </a>
        </span>
      </advance-table>
    </a-card>
    <a-modal :visible="visible" title="浏览图片" @cancel="close" :width="600">
      <div style="display:flex;padding:20px;justify-content: space-between;">
        <div v-for="(attach,index) in images" :key="index">
          <img style="width:200px;height:200px" :src="attach.attachPath"/>
        </div>
      </div>
    </a-modal>
  </div>
</template>
<script>
import { listBizAfterSaleOrder } from '@/api/biz/bizAfterSaleOrder'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import BizAfterSaleOrderForm from '@/views/biz/bizaftersale/modules/BizAfterSaleOrderForm.vue'
import BizAfterSaleOrderUser from '@/views/biz/bizaftersale/modules/BizAfterSaleOrderUser.vue'
import tableDragResizeMixin from '@/mixins/tableDragResizeMixin'
import BizOrderAddForm from '@/views/biz/bizorder/modules/BizOrderAddForm.vue'
import { listAllBizCustomer } from '@/api/biz/bizCustomer'
import listMixin from '@/mixins/listMixin';

export default {
  name: 'BizPayment',
  components: {
    AdvanceTable,
    BizAfterSaleOrderForm,
    BizAfterSaleOrderUser,
    BizOrderAddForm
  },
  mixins:[tableDragResizeMixin,listMixin],
  data() {
    return {
      showAddModal: false,
      showOrderViewModal: false,
      title:'售后单',
      listFunc:(param)=>listBizAfterSaleOrder(param),
      // 遮罩层
      loading: true,
      visible: false,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      images:[],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 表格数据
      dataList: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        supplierName: undefined
      },
      // 客户字典
      customerIdOptions: [],
      columns: [
        {
          title: '售后单号',
          dataIndex:'saleOrderCode',
          align: 'center'
        },
        {
          title: '售后日期',
          dataIndex:'saleOrderDate',
          align: 'center'
        },
        {
          title: '关联销售单',
          dataIndex:'orderNumber',
          align: 'center',
          scopedSlots: { customRender: 'showOrder' },
        },
        {
          title: '客户',
          dataIndex:'customerName',
          align: 'center'
        },
        {
          title: '客户地址',
          dataIndex:'address',
          align: 'center'
        },
        {
          title: '业主电话',
          dataIndex:'phone',
          align: 'center'
        },
        {
          title: '问题描述',
          dataIndex:'remark',
          align: 'center'
        },
        {
          title: '处理跟进',
          dataIndex:'remarkA',
          align: 'center'
        },
        {
          title: '收费金额',
          dataIndex:'feedAmount',
          align: 'center'
        },
        {
          title: '安装照片',
          align: 'center',
          scopedSlots: { customRender: 'inImg' },
        },
        {
          title: '安装师傅',
          dataIndex:'installUser',
          align: 'center'
        },
        {
          title: '客户反馈照片',
          align: 'center',
          scopedSlots: { customRender: 'cuImg' },
        },
        {
          title: '售后处理照片',
          align: 'center',
          scopedSlots: { customRender: 'saleImg' },
        },
        {
          title: '处理人',
          align: 'center',
          scopedSlots: { customRender: 'userSlot' }
        },
        {
          title: '状态',
          dataIndex:'status',
          align: 'center',
          scopedSlots: { customRender: 'statusSlot' }
        },
        {
          title: '操作',
          align: 'center',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created() {
    this.getList()
    listAllBizCustomer().then((response) => {
      this.customerIdOptions = response.data
    })
  },
  methods: {
    /** 查询销售单列表 */
    getList() {
      this.loading = true
      listBizAfterSaleOrder(this.queryParam).then(response => {
        this.dataList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    handleShow(record,flag){
      if(flag==1) {
        this.visible = true;
        this.images=record.installerAttaches;
      } else if(flag == 2) {
        this.visible = true;
        this.images = record.saleOrderAttaches
      } else if(flag == 3) {
        this.visible = true;
        this.images = record.afterSaleOrderAttaches
      } else if(flag==4) {
        this.showOrderViewModal=true;
        this.$nextTick(()=>{
          this.$refs.bizOrderAddForm.handleViewById(record.orderId)
        })
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        supplierName: undefined

      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    handleAdd(record) {
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizAfterSaleOrderForm.handleAdd(record)
      ))
    },
    handleUpdate(record){
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizAfterSaleOrderForm.handleAdd(record)
      ))
    },
    handleUser(record){
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizAfterSaleOrderUser.handleUser(record)
      ))
    },
    close(){
      this.visible = false;
      this.images = []
      this.getList()
    },
    handleTableChange(pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    },
    customRow (record) {
      return {
        on: {
          dblclick: () => {
            this.$nextTick(() => {
              if(record.id){
                this.handleUpdate(record)
              }
            })
          }
        }
      }
    }
  }
}
</script>
